<template>
  <div>
    <!-- 查询区域 -->
    <div class="card" style="margin-bottom: 5px;">
      <el-input
        v-model="title"
        style="width: 300px; margin-right: 10px"
        placeholder="请输入菜单名称查询"
      ></el-input>
      <el-button type="primary" @click="load">查询</el-button>
      <el-button type="info" style="margin: 0 10px" @click="reset">重置</el-button>
    </div>

    <!-- 新增按钮 -->
    <div class="card" style="margin-bottom: 5px">
      <div style="margin-bottom: 10px">
        <el-button type="primary" @click="handleAdd">新增菜单</el-button>
      </div>

      <!-- 表格 -->
      <el-table :data="tableData" stripe>
        <el-table-column label="菜单编号" prop="id" width="120" />
        <el-table-column label="菜单名称" prop="title" />
        <el-table-column label="路径" prop="path" />
        <el-table-column label="图标" prop="icon" />
        <el-table-column label="父级菜单" prop="parent" />
        <el-table-column label="创建时间" prop="createTime" />
        <el-table-column label="操作" align="center" width="260">
          <template slot-scope="scope">
            <el-button type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="card">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="pageSize"
        :current-page="pageNum"
        :total="total"
        @current-change="handlePageChange"
      />
    </div>

    <!-- 新增/编辑菜单对话框 -->
    <el-dialog
      title="菜单信息"
      width="40%"
      :visible.sync="formVisible"
      :close-on-click-modal="false"
    >
      <el-form :model="form" label-width="100px" style="padding-right: 50px">
        <el-form-item label="菜单名称" prop="title">
          <el-input v-model="form.title" autocomplete="off" />
        </el-form-item>
        <el-form-item label="路径" prop="path">
          <el-input v-model="form.path" autocomplete="off" />
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="form.icon" autocomplete="off" placeholder="如 el-icon-menu" />
        </el-form-item>
        <el-form-item label="父级菜单" prop="parent">
          <el-select v-model="form.parent" placeholder="请选择父级菜单">
            <el-option label="无" value="无"></el-option>
            <el-option label="系统管理" value="系统管理"></el-option>
            <el-option label="用户管理" value="用户管理"></el-option>
            <el-option label="角色管理" value="角色管理"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <template slot="footer">
        <span class="dialog-footer">
          <el-button @click="formVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">保 存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// import request from "@/utils/request"
import { MessageBox, Message } from "element-ui";

export default {
  data() {
    return {
      pageNum: 1,
      pageSize: 5,
      total: 0,
      title: null,
      formVisible: false,
      form: {},
      tableData: [],
      // ✅ 模拟数据
      mockMenus: [
        { id: 1, title: "首页", path: "/home", icon: "el-icon-house", parent: "无", createTime: "2025-10-01" },
        { id: 2, title: "用户管理", path: "/user", icon: "el-icon-user", parent: "系统管理", createTime: "2025-10-03" },
        { id: 3, title: "角色管理", path: "/role", icon: "el-icon-s-custom", parent: "系统管理", createTime: "2025-10-05" },
        { id: 4, title: "菜单管理", path: "/menu", icon: "el-icon-menu", parent: "系统管理", createTime: "2025-10-06" },
        { id: 5, title: "日志查看", path: "/log", icon: "el-icon-document", parent: "系统管理", createTime: "2025-10-10" }
      ]
    };
  },
  created() {
    // ✅ 确保组件加载时表格数据赋值，页面显示正常
    this.load();
  },
  methods: {
    /** 加载分页数据 */
    load() {
      let filtered = this.title
        ? this.mockMenus.filter(r => r.title.includes(this.title))
        : [...this.mockMenus];
      this.total = filtered.length;
      // 修正 pageNum，防止 slice 空数组
      this.pageNum = Math.min(this.pageNum, Math.ceil(filtered.length / this.pageSize) || 1);
      const start = (this.pageNum - 1) * this.pageSize;
      const end = start + this.pageSize;
      this.tableData = filtered.slice(start, end);
    },

    /** 重置 */
    reset() {
      this.title = null;
      this.pageNum = 1;
      this.load();
    },

    /** 翻页 */
    handlePageChange(val) {
      this.pageNum = val;
      this.load();
    },

    /** 新增 */
    handleAdd() {
      this.form = {};
      this.formVisible = true;
    },

    /** 编辑 */
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.formVisible = true;
    },

    /** 保存 */
    save() {
      if (this.form.id) {
        this.update();
      } else {
        this.add();
      }
    },

    /** 新增菜单（模拟） */
    add() {
      const newMenu = {
        ...this.form,
        id: this.mockMenus.length + 1,
        createTime: new Date().toISOString().slice(0, 10)
      };
      this.mockMenus.push(newMenu);
      Message.success("新增成功（模拟）");
      this.formVisible = false;
      this.load();
    },

    /** 更新菜单（模拟） */
    update() {
      const idx = this.mockMenus.findIndex(r => r.id === this.form.id);
      if (idx !== -1) {
        this.mockMenus.splice(idx, 1, this.form);
      }
      Message.success("更新成功（模拟）");
      this.formVisible = false;
      this.load();
    },

    /** 删除菜单（模拟） */
    handleDelete(id) {
      MessageBox.confirm("删除后不可恢复，是否确认删除？", "提示", {
        type: "warning"
      }).then(() => {
        this.mockMenus = this.mockMenus.filter(r => r.id !== id);
        Message.success("删除成功（模拟）");
        this.load();
      });
    }
  }
};
</script>

<style scoped>
.card {
  background: #fff;
  padding: 15px;
  border-radius: 10px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
</style>
